package com.jack.lib.ui.adapter

import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import androidx.annotation.StringRes
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jack.lib.ui.R
import com.jack.lib.ui.widget.JEditText
import com.jack.lib.ui.widget.JImageView
import com.jack.lib.ui.widget.JTextView
import java.io.File

/**
 * author : chong.huang
 * create : 2021/12/23
 * desc   :
 */
open class JViewHolder(view: View): BaseViewHolder(view) {

    override fun setText(@IdRes viewId: Int, @StringRes strId: Int): BaseViewHolder? {
        try {
            super.setText(viewId, strId)
        } catch (e: Resources.NotFoundException) {
            super.setText(viewId, strId.toString())
        }
        return this
    }

    fun getJTV(@IdRes id: Int): JTextView {
        return getView(id)
    }

    fun getJIV(@IdRes id: Int): JImageView {
        return getView(id)
    }


    fun getString(@IdRes id: Int): String {
        return getTV(id).text.toString()
    }


    fun loadImageUrl(@IdRes id: Int, url: String?) {
        loadImageUrl(id, url, 0)
    }

    fun loadAvatar(@IdRes id: Int, url: String?) {
        loadImageUrl(id, url, R.drawable.default_avatar)
    }

    fun loadImageUrl(@IdRes id: Int, url: String?, @DrawableRes perch: Int) {
        val options = RequestOptions()
            .error(perch)
            .placeholder(perch)
        Glide.with(itemView)
            .load(url)
            .apply(options)
            .into(getIV(id))
    }


    open fun loadImage(@IdRes viewId: Int, url: String?, placeholderId: Int = 0, errorId: Int = 0) = this.apply {
        Glide.with(itemView.context).load(url).placeholder(placeholderId).error(errorId).into(getView(viewId))
    }

    open fun loadImage(@IdRes viewId: Int, drawable: Drawable?, placeholderId: Int = 0, errorId: Int = 0) = this.apply {
        Glide.with(itemView.context).load(drawable).placeholder(placeholderId).error(errorId).into(getView(viewId))
    }

    open fun loadImage(@IdRes viewId: Int, file: File?, placeholderId: Int = 0, errorId: Int = 0) = this.apply {
        Glide.with(itemView.context).load(file).placeholder(placeholderId).error(errorId).into(getView(viewId))
    }

    open fun loadImage(@IdRes viewId: Int, resourceId: Int?, placeholderId: Int = 0, errorId: Int = 0) = this.apply {
        Glide.with(itemView.context).load(resourceId).placeholder(placeholderId).error(errorId).into(getView(viewId))
    }

    open fun setSelect(@IdRes viewId: Int, select: Boolean) = this.apply {
        getView<View>(viewId).isSelected = select
    }

    open fun setOnClickListener(@IdRes viewId: Int, onClick: View.OnClickListener?) = this.apply {
        getView<View>(viewId).setOnClickListener(onClick)
    }

    fun getIV(@IdRes viewId: Int): ImageView = getView(viewId)

    fun getTV(@IdRes viewId: Int): TextView = getView(viewId)
}